﻿using System;
using System.Data;
using System.IO;
using Castle.Core;
using Castle.Core.Configuration;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Configuration;
using Castle.MonoRail.Framework.Container;
using Castle.MonoRail.Framework.Internal;
using Castle.MonoRail.ViewComponents;
using Castle.MonoRail.Views.Brail;
using Yama.HelloWorld.Dao.NHibernate;
using Yama.MVC.MonoRail.Services;
using Yama.MVC.MonoRail.Extensions;
using Yama.MVC.NHibernate.Connections;

namespace Yama.HelloWorld.Web
{
    public class Global : Yama.MVC.Container.WindsorHttpApplication, IMonoRailConfigurationEvents, IMonoRailContainerEvents
    {
        #region Overrides of WindsorHttpApplication

        public override void ApplicationStart(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            base.ApplicationStart(sender, e);
        }

        public override void WindsorSetup(Yama.MVC.Container.Container container)
        {
            container.RegisterControllers("Yama.HelloWorld.Rails");
            container.RegisterViewComponents("Yama.HelloWorld.Rails");
            container.RegisterViewComponents("Castle.Monorail.ViewComponents");

            // Todo: If you want to use Yama's RepositoryFactory add repositories 
            // by calling container#RegisterRepositories(IRepositoriesProvider, LifestyleType)
            container.RegisterRepositories(new RepositoriesProvider(), LifestyleType.PerWebRequest);

            container.AddComponent("WebSessionProvider",
                typeof(ISessionProvider),
                typeof(NHibernateIntegrationSessionProvider));

            // Todo: Add here any additional Windsor components 

            SessionProviderFactory.GetInstance = container.Resolve<ISessionProvider>;
        }

        #endregion

        #region Implementation of IMonoRailConfigurationEvents

        public void Configure(IMonoRailConfiguration configuration)
        {
            // programatic Brail configurtion
            configuration.ViewEngineConfig.ViewPathRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views");
            configuration.ViewEngineConfig.ViewEngines.Add(new ViewEngineInfo(typeof(BooViewEngine), true));

            // additional view sources
            configuration.ViewEngineConfig.AssemblySources.Add(new AssemblySourceInfo("Yama.MVC.MonoRail", "Yama.MVC.MonoRail.Views"));

            // add controllers and viewcomponents
            configuration.ControllersConfig.AddAssembly(System.Reflection.Assembly.Load("Yama.HelloWorld.Rails"));
            configuration.ControllersConfig.AddAssembly(typeof(GridComponent).Assembly);

            // add extensions
            configuration.ExtensionEntries.Add(new ExtensionEntry(typeof(AjaxRequestExtension), new MutableConfiguration("")));
            configuration.ExtensionEntries.Add(new ExtensionEntry(typeof (TransactionManagementExtension),
                                                                  new MutableConfiguration("")
                                                                      .Attribute("TransactionalActionHandlerType",
                                                                                 typeof (NhibernateTransactionHandler).
                                                                                     AssemblyQualifiedName)
                                                                      .Attribute("defaultIsolationLevel",
                                                                                 IsolationLevel.ReadCommitted.ToString())));
        }

        #endregion

        #region Implementation of IMonoRailContainerEvents

        public void Created(IMonoRailContainer container)
        {
        }

        public void Initialized(IMonoRailContainer container)
        {
            // Uncomment below to add a replacement fo IJsProxyGenerator, which always does asynchronous AJAX calls
            container.AjaxProxyGenerator = new ForceAsyncPrototypeAjaxProxyGenerator();
        }

        #endregion
    }
}